package com.example.musicaiap.service

import android.annotation.SuppressLint
import android.content.Context
import androidx.core.os.bundleOf
import androidx.media3.common.C
import androidx.media3.common.Player
import androidx.media3.session.CommandButton
import androidx.media3.session.DefaultMediaNotificationProvider
import androidx.media3.session.MediaSession
import androidx.media3.session.SessionCommand
import com.example.musicaiap.R
import com.google.common.collect.ImmutableList

@SuppressLint("UnsafeOptInUsageError")
class MediaNotificationProvider(
    context: Context
) : DefaultMediaNotificationProvider(
    context, { MusicService.NOTIFICATION_ID },
    MusicService.CHANNEL_ID,
    DEFAULT_CHANNEL_NAME_RESOURCE_ID
) {

    override fun getMediaButtons(
        session: MediaSession,
        playerCommands: Player.Commands,
        customLayout: ImmutableList<CommandButton>,
        showPauseButton: Boolean
    ): ImmutableList<CommandButton> {

        // Skip to previous action.
        val commandButtons = ImmutableList.Builder<CommandButton>()
        val commandButtonExtras = bundleOf(COMMAND_KEY_COMPACT_VIEW_INDEX to C.INDEX_UNSET)
        for (i in customLayout.indices) {
            val button = customLayout[i]
            if (button.sessionCommand != null
                && button.sessionCommand!!.commandCode == SessionCommand.COMMAND_CODE_CUSTOM
                && (button.sessionCommand!!.customAction == MusicService.CUSTOM_COMMAND_TOGGLE_LIKE_ON ||
                        button.sessionCommand!!.customAction == MusicService.CUSTOM_COMMAND_TOGGLE_LIKE_OFF)
            ) {
                commandButtons.add(button)
                break
            }
        }
        commandButtons.add(
            CommandButton.Builder()
                .setPlayerCommand(Player.COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM)
                .setIconResId(R.drawable.player_ico_notify_player_prev)
                .setDisplayName("上一首")
                .setExtras(commandButtonExtras)
                .build()
        )
        commandButtons.add(
            CommandButton.Builder()
                .setPlayerCommand(Player.COMMAND_PLAY_PAUSE)
                .setIconResId(
                    if (showPauseButton) R.drawable.player_ico_notify_player_pause else R.drawable.player_ico_notify_player_play
                )
                .setExtras(commandButtonExtras)
                .setDisplayName(
                    if (showPauseButton) "暂停" else "播放"
                )
                .build()
        )
        // Skip to next action.
        commandButtons.add(
            CommandButton.Builder()
                .setPlayerCommand(Player.COMMAND_SEEK_TO_NEXT_MEDIA_ITEM)
                .setIconResId(R.drawable.player_ico_notify_player_next)
                .setExtras(commandButtonExtras)
                .setDisplayName("下一首")
                .build()
        )
        for (i in customLayout.indices) {
            val button = customLayout[i]
            if (button.sessionCommand != null
                && button.sessionCommand!!.commandCode == SessionCommand.COMMAND_CODE_CUSTOM
                && (button.sessionCommand!!.customAction == MusicService.CUSTOM_COMMAND_TOGGLE_LYRIC_ON ||
                        button.sessionCommand!!.customAction == MusicService.CUSTOM_COMMAND_TOGGLE_LYRIC_OFF)
            ) {
                commandButtons.add(button)
                break
            }
        }
        return commandButtons.build()
    }


}
